package ua.com.kangaroo.parser;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

import ua.com.kangaroo.basic.SchoolsData;

public class BaseParser {
	private File baseFile;
	
	public BaseParser(String baseFileName) {
		baseFile = new File(baseFileName);
	}
	
	/**
	 * 
	 * @param ArrayOfString String massive from base.dat file
	 * @param position position of first element in old massive
	 * @return new massive of int
	 */
	private int[] getArray(String[] ArrayOfString, int position){
		int[] result = new int[12];
		if (!ArrayOfString[position].equals("")){
			result[2] = Integer.parseInt(ArrayOfString[position]);
		} else result[2] = 0;
		if (!ArrayOfString[position + 1].equals("")){
		    result[3] = Integer.parseInt(ArrayOfString[position + 1]);
		} else result[3] = 0;
		if (!ArrayOfString[position + 2].equals("")){
		    result[4] = Integer.parseInt(ArrayOfString[position + 2]);
		} else result[4] = 0;
		if (!ArrayOfString[position + 4].equals("")){
		    result[5] = Integer.parseInt(ArrayOfString[position + 4]);
		} else result[5] = 0;
		if (!ArrayOfString[position + 5].equals("")){
		    result[6] = Integer.parseInt(ArrayOfString[position + 5]);
		} else result[6] = 0;
		if (!ArrayOfString[position + 7].equals("")){
		    result[7] = Integer.parseInt(ArrayOfString[position + 7]);
		} else result[7] = 0;
		if (!ArrayOfString[position + 8].equals("")){
		    result[8] = Integer.parseInt(ArrayOfString[position + 8]);
		} else result[8] = 0;
		if (!ArrayOfString[position + 11].equals("")){
		    result[9] = Integer.parseInt(ArrayOfString[position + 11]);
		} else result[9] = 0;
		if (!ArrayOfString[position + 12].equals("")){
		    result[10] = Integer.parseInt(ArrayOfString[position + 12]);
		} else result[10] = 0;
		if (!ArrayOfString[position + 14].equals("")){
		    result[11] = Integer.parseInt(ArrayOfString[position + 14]);
		} else result[11] = 0;
		return result;
	}
	
	
	/**
	 * @return false if there exist some problem to parse baseFile and true if not 
	 */
	
	public boolean readFromFile(){
		Scanner FileScanner = null;
		try{
			FileScanner = new Scanner(new BufferedReader(new FileReader(baseFile)));
			FileScanner.useDelimiter("[\r\n]+");
			while (FileScanner.hasNext()){
				int id;
				int[] uk = new int[12];
				int[] ru = new int[12];
				int[] en = new int[12];
				double earn;
				double debt; 
				
				if (FileScanner.hasNextInt()){
					id = FileScanner.nextInt();
				} else return false;
				
				if (FileScanner.hasNext()){
					String participants = FileScanner.next();
					String[] lineArray = participants.split("[,]{1}");
					if (lineArray.length < 16) {
						for(int i = 1; i < 12; i++) {
							uk[i] = 0;
							ru[i] = 0;
							en[i] = 0;
						}
						earn = 0;
						debt = 0;
					} else {
						uk = getArray(lineArray, 0);
						if (lineArray.length < 36) {
							for (int i = 1; i < 12; i++) {
								ru[i] = 0;
								en[i] = 0;
							}
							earn = 0;
							debt = 0;
						} else {
							ru = getArray(lineArray, 17);
							if (!lineArray[34].equals("")) {
								earn = Double.parseDouble(lineArray[34]);
							} else {
								earn = 0;
							}
							if (!lineArray[34].equals("")) {
								debt = Double.parseDouble(lineArray[35]);
							} else {
								debt = 0;
							}
							if (lineArray.length < 53) {
								for(int i = 1; i < 12; i++) {
									en[i] = 0;
								}
							} else {
								en = getArray(lineArray, 37);
							}
						}
			 		}
				
				} else return false;
			if (!SchoolsData.addParticipants(id, uk, ru, en, earn, debt)) return false;
			} 
			return true;
		} catch (IOException e) {
			return false;
		} finally {
			if (FileScanner != null) FileScanner.close();
		}
	}
}
